package cn.ncupyh.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import static java.sql.DriverManager.getConnection;

public class JDBCUtils {
    private static   String uri = "jdbc:mysql://127.0.0.1:3306/21soft?serverTimezone=GMT%2B8&userSSL=false";
    private static  String root = "root";
    private static  String password = "djx020601";
    private static Connection conn = null;
    static{
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = getConnection(uri,root,password);
        } catch (Exception e) {
            throw new RuntimeException("数据库连接失败");
        }
    }
    //获取连接
    public static Connection getConn(){
        return  conn;
    }
    // 释放连接
    public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) throws SQLException {
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        rs = null;
        release(conn,pstmt);
    }

    public static void release(Connection conn, PreparedStatement pstmt) throws SQLException {
        if(pstmt != null){
            try {
                pstmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        pstmt = null;
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            conn = null;
        }
    }
}
